package com.tensquare.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.SmsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by WF on 2019/9/21 10:48
 */
@Component
@RabbitListener(queues = "sms")
public class SmsListener {
    @Autowired
    private SmsUtil smsUtil;
    //接受到后台发来的验证码相关信息,就向阿里大于发送短信息
    @RabbitHandler
    public void getMessage(Map map){
        try {
            //1.得到手机号
            String mobile = map.get("mobile").toString();
            //2.得到模板code
            String templateCode = "SMS_148593167";
            //3.得到签名
            String signName = "品优购";
            //4.定义参数
            String code = map.get("code").toString();
            Map map1 = new HashMap();
            map1.put("code",code);
            String param = JSON.toJSONString(map1);
            //5.向阿里大于发送消息
            smsUtil.sendSms(mobile,templateCode,signName,param);

            System.out.println("验证码：" + code);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
